<template>
  <PageContainer title="用户管理" description="管理平台上的所有用户">
    <div class="admin-page-content">
      <!-- 搜索表单 -->
      <GlassCard class="search-card">
      <template #header>
        <h3 class="search-title">筛选条件</h3>
      </template>
      <a-form layout="inline" :model="searchParams" @finish="doSearch" class="search-form">
        <a-form-item label="账号">
          <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" class="search-input" />
        </a-form-item>
        <a-form-item label="用户名">
          <a-input v-model:value="searchParams.userName" placeholder="输入用户名" class="search-input" />
        </a-form-item>
        <a-form-item>
          <GlassButton type="primary" size="small" html-type="submit">搜索</GlassButton>
        </a-form-item>
        <a-form-item>
          <GlassButton type="secondary" size="small" @click="doReset">重置</GlassButton>
        </a-form-item>
      </a-form>
    </GlassCard>

    <!-- 表格 -->
    <GlassTable
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      :loading="loading"
      @change="doTableChange"
      :scroll="{ x: 950 }"
      class="user-table"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <div class="avatar-cell">
            <a-image :src="record.userAvatar" :width="60" :height="60" class="user-avatar" />
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">普通用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-popconfirm title="确定要删除这个用户吗？" @confirm="doDelete(record.id)">
            <GlassButton type="danger" size="small">删除</GlassButton>
          </a-popconfirm>
        </template>
      </template>
    </GlassTable>
    </div>
  </PageContainer>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUser, listUserVoByPage } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import PageContainer from '@/components/base/PageContainer.vue'
import GlassCard from '@/components/base/GlassCard.vue'
import GlassButton from '@/components/base/GlassButton.vue'
import GlassTable from '@/components/base/GlassTable.vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 100,
    fixed: 'left',
    align: 'center',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
    width: 120,
    ellipsis: true,
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    width: 120,
    ellipsis: true,
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
    width: 80,
    align: 'center',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
    width: 200,
    ellipsis: true,
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
    width: 100,
    align: 'center',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 160,
    align: 'center',
  },
  {
    title: '操作',
    key: 'action',
    width: 100,
    fixed: 'right',
    align: 'center',
  },
]

// 展示的数据
const data = ref<API.UserVO[]>([])
const total = ref(0)
const loading = ref(false)

// 搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const res = await listUserVoByPage({
      ...searchParams,
    })
    if (res.data.data) {
      data.value = res.data.data.records ?? []
      total.value = res.data.data.totalRow ?? 0
    } else {
      message.error('获取数据失败，' + res.data.message)
    }
  } catch (error) {
    message.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格分页变化时的操作
const doTableChange = (page: { current: number; pageSize: number }) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

// 重置
const doReset = () => {
  searchParams.userAccount = ''
  searchParams.userName = ''
  searchParams.pageNum = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUser({ id: Number(id) })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.search-card {
  margin-bottom: 24px;
}

.search-title {
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-weight: 500;
}

.search-form {
  margin-top: 16px;
  flex-wrap: wrap;
  gap: 8px;
}

.search-form .ant-form-item {
  margin-bottom: 16px;
}

.search-form :deep(.ant-form-item-label > label) {
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
}

.search-form :deep(.ant-input) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: #ffffff;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.search-form :deep(.ant-input::placeholder) {
  color: rgba(255, 255, 255, 0.5);
}

.search-form :deep(.ant-input:focus) {
  border-color: #60a5fa;
  box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

/* 搜索框尺寸优化 */
.search-input {
  width: 200px !important;
}

.search-form :deep(.ant-form-item) {
  margin-right: 16px;
  margin-bottom: 16px;
}

.admin-page-content {
  max-width: 85%;
  margin: 0 auto;
}

.user-table {
  min-height: 400px;
}

.avatar-cell {
  display: flex;
  justify-content: center;
  align-items: center;
}

.user-avatar {
  border-radius: 50%;
  object-fit: cover;
}

/* 自定义滚动条样式 */
:deep(.ant-table-body) {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
}

:deep(.ant-table-body::-webkit-scrollbar) {
  width: 8px;
  height: 8px;
}

:deep(.ant-table-body::-webkit-scrollbar-track) {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
}

:deep(.ant-table-body::-webkit-scrollbar-thumb) {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 4px;
}

:deep(.ant-table-body::-webkit-scrollbar-thumb:hover) {
  background: rgba(255, 255, 255, 0.5);
}

/* 表格行悬停效果 */
:deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(255, 255, 255, 0.05) !important;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }

  .search-form .ant-form-item {
    margin-bottom: 8px;
    margin-right: 0;
  }
}

@media (max-width: 768px) {
  .search-form .ant-form-item-label {
    text-align: left;
    padding-bottom: 4px;
  }

  .user-table {
    font-size: 12px;
  }

  .user-avatar {
    width: 40px !important;
    height: 40px !important;
  }
}

@media (max-width: 480px) {
  .user-table {
    font-size: 11px;
  }

  .user-avatar {
    width: 32px !important;
    height: 32px !important;
  }
}
</style>
